package com.wondedDream.repository;

import com.wondedDream.model.UserPracticeRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserPracticeRecordRepository extends JpaRepository<UserPracticeRecord, Integer> {
    // 新增的自定义查询方法
    @Query("SELECT p FROM UserPracticeRecord p WHERE p.user.id = :userId")
    List<UserPracticeRecord> findByUserId(Integer userId);

    //根据问题id查询当前用户记录
    @Query("SELECT p FROM UserPracticeRecord p WHERE p.practiceQuestion.id = :practiceQuestionId")
    List<UserPracticeRecord> findByPracticeQuestionId(Integer practiceQuestionId);

    @Query("SELECT new com.wondedDream.model.UserPracticeRecord(p.id, p.createTime) FROM UserPracticeRecord p WHERE p.id IN (SELECT DISTINCT pr.id FROM UserPracticeRecord pr) ORDER BY p.createTime DESC")
    List<UserPracticeRecord> findDistinctPracticeIdsByOpenid(String userOpenId);
}